let data = {};
let editingId = null;
let requestUrl = null;

function addHandler() {
    $("#pattern_dialog").dialog("open");

    requestUrl = "/potency_fruit_pattern_category/insert";
}

function editHandler() {
    let data = $("#pattern_list").datagrid("getSelected");

    if(data) {
        $("#id").val(data.id);
        $("#name").textbox("setValue", data.name);
        $("#type").combobox("setValue", data.type);
        $("#comboEffect").textbox("setValue", data.comboEffect);

        $("#pattern_dialog").dialog("open");

        requestUrl = "/potency_fruit_pattern_category/updateById";
    } else {
        alertMsg("请选择要修改的记录！", "info");
    }
}

function effectHandler() {
    let rowData = $("#pattern_list").datagrid("getSelected");

    if (rowData) {
        $("#effect_list").datagrid("reload", {
            potencyId: rowData.id
        });

        $("#effect_dialog").dialog("open");
    } else {
        unselected();
    }
}

function imageHandler() {
    let rowData = $("#pattern_list").datagrid("getSelected");

    if (rowData) {
        $("#pattern_id").val(rowData.id);
        $("#img").attr("src", rowData.image);

        $("#upload_dialog").dialog("open");
    } else {
        alertMsg("请选择要修改的记录。", "warning");
    }
}

$(document).ready(function () {

    $("#_type_").combobox({
        url: "/potency_fruit_pattern_type/selectAll",
        valueField: "id",
        textField: "name",
        width: 150,
        panelHeight: "auto",
        prompt: "-图案类型-"
    });

    $("#search").linkbutton({
        iconCls: "icon-search"
    }).click(function() {
        let type = $("#_type_").combobox("getValue");

        $("#pattern_list").datagrid("load", {
            type: type
        });
    });

    $("#clear").linkbutton({
        iconCls: "icon-delete"
    }).click(function () {
        $("#search_form").form("clear");
    });


    $("#type").combobox({
        url: "/potency_fruit_pattern_type/selectAll",
        valueField: "id",
        textField: "name",
        width: 120,
        required: true,
        panelHeight: "auto"
    });

    $("#name").textbox({
        width: 120,
        required: true
    });

    $("#comboEffect").textbox({
        width: 314,
        height: 80,
        required: true,
        multiline: true
    });

    $("#pattern_dialog").dialog({
        title: "潜能图案信息",
        width: 500,
        height: 300,
        buttons: [{
            iconCls: "icon-save",
            text: "保存",
            handler: function() {
                let selector = "#pattern_form";

                checkForm(selector, function () {
                    let data = $(selector).serialize();

                    ajaxPost(requestUrl, data, function(response) {
                        showMsg(response.message);

                        $(selector).form("clear");
                        $("#pattern_dialog").dialog("close");
                        $("#pattern_list").datagrid("reload");
                    }, error);
                });
            }
        }, {
            iconCls: "icon-cancel",
            text: "取消",
            handler: function() {
                $("#pattern_dialog").dialog("close");
                $("#pattern_form").form("clear");
            }
        }]
    });


    $("#effect_list").datagrid({
        url: "/potency_fruit_pattern_effect/selectByPage",
        title: "图案效果",
        width: 500,
        height: 300,
        pagination: true,
        pageSize: 5,
        pageList: [5, 10, 20, 50],
        onAfterEdit: function (rowIndex, rowData, changes) { // 结束行内编辑事件
            data = {
                id: rowData.id,
                potencyId: rowData.potencyId,
                propertyId: changes.propertyId ? changes.propertyId : rowData.propertyId,
                propertyValue: changes.propertyValue ? changes.propertyValue : rowData.propertyValue
            };
        },
        toolbar: [{
            iconCls: "icon-add",
            text: "添加",
            handler: function() {
                let rowData = $("#pattern_list").datagrid("getSelected");

                if(rowData) {
                    let datagrid = $("#effect_list");

                    datagrid.datagrid("appendRow",{
                        id: null,
                        potencyId: rowData.id,
                        propertyId: null,
                        propertyValue: 0
                    });

                    let total = datagrid.datagrid("getRows").length;

                    editingId = total - 1;
                    requestUrl = "/potency_fruit_pattern_effect/insert";

                    datagrid.datagrid("beginEdit", editingId);
                } else {
                    unselected("请选择一件装备~");
                }
            }
        }, "-", {
            iconCls: "icon-edit",
            text: "修改",
            handler: function() {
                let datagrid = $("#effect_list");

                if (editingId != null) {
                    datagrid.datagrid("selectRow", editingId);
                    datagrid.datagrid("beginEdit", editingId);
                } else {
                    let rowData = datagrid.datagrid("getSelected");

                    if (rowData) {
                        // 获取行索引，这个索引从0开始
                        editingId = datagrid.datagrid("getRowIndex", rowData);
                        requestUrl = "/potency_fruit_pattern_effect/updateById";

                        datagrid.datagrid("beginEdit", editingId);
                    } else {
                        unselected();
                    }
                }
            }
        }, "-", {
            iconCls: "icon-save",
            text: "保存",
            handler: function() {
                if (editingId != null) {
                    // 只有结束编辑才能获取到最新的值
                    $("#effect_list").datagrid("endEdit", editingId);

                    ajaxPost(requestUrl, data, function(resp) {
                        editingId = null;
                        
                        showMsg(resp.message);

                        $("#effect_list").datagrid("reload");
                    }, error);
                }
            }
        }, "-", {
            iconCls: "icon-cancel",
            text: "取消",
            handler: function() {
                if (editingId != null) {
                    $("#effect_list").datagrid("cancelEdit", editingId);

                    editingId = null;
                }
            }
        }, "-", {
            iconCls: "icon-delete",
            text: "删除",
            handler: function() {
                let rowData = $("#effect_list").datagrid("getSelected");

                if(rowData) {
                    $.messager.confirm("提示", "是否确认删除？", function(bool) {
                        if (bool) {
                            ajaxPost("/potency_effect/deleteById", {
                                id: rowData.id
                            }, function(resp) {
                                showMsg(resp.message);

                                $("#effect_list").datagrid("reload");
                            }, error);
                        }
                    });
                } else {
                    alertMsg("请选择一条记录！", "warning");
                }
            }
        }],
        columns: [[
            {field: "id", title: "编号", align: "center", hidden: true},
            {field: "propertyId", title: "属性", align: "center", width: 150
                , editor: {
                    type: "combobox",
                    options: {
                        url: "/property/selectAll",
                        valueField: "id",
                        textField: "name",
                        panelHeight: "auto"
                    }
                }
                , formatter: function(value, rowData, rowIndex) {
                    if (value) {
                        ajaxGet("/property/selectById", {
                            id: value
                        }, function (resp) {
                            let data = resp.data;

                            $("#property_" + rowIndex).html(data.name);
                        }, error);

                        return "<div class='blue' id='property_" + rowIndex + "'></div>";
                    }
                }
            },
            {field: "propertyValue", title: "属性值", align: "center", width: 150, editor: {
                    type: "numberspinner",
                    options: {
                        min: 1
                    }
                }},
            {field: "potencyId", title: "潜能图案类型", hidden: true}
        ]]
    });

    $("#effect_dialog").dialog({
        title: " ",
        height: 340
    });

    $("#upload").filebox({
        buttonText: "选择文件",
        width: 240,
        required: true,
        onChange: function() {
            let rowData = $("#pattern_list").datagrid("getSelected");

            if(rowData) {
                fileUpload(this, rowData.id, "/potency_fruit_pattern_category/upload");
            }
        }
    });

    fetchUploadDialog("/potency_fruit_pattern_category/updateById", function () {
        $("#pattern_list").datagrid("reload");
    });

    /******************************************************************************************************/

    /**
     * 表格体的高度
     * @type {number}
     */
    let height_body = heigt_tr_with_image * 10;
    /**
     * 表格高度
     * @type {number}
     */
    let height_table = heigt_tool + heigt_header + height_body + heigt_footer + heigt_scoll_bar + 2;

    $("#pattern_list").datagrid({
        url: "/potency_fruit_pattern_category/selectByPage",
        height: height_table,
        fitColumns: true,
        pagination: true,
        toolbar: [{
            iconCls: "icon-add",
            text: "添加",
            handler: function() {
                addHandler();
            }
        }, "-", {
            iconCls: "icon-edit",
            text: "修改",
            handler: function() {
                editHandler();
            }
        }, "-", {
            iconCls: "icon-shield",
            text: "效果",
            handler: function() {
                effectHandler();
            }
        }, "-", imageUpdateButton],
        columns: [[
            {field: "id", title: "编号", align:"center"},
            {field: "name", title: "组合名称", align:"center", width: 100},
            {field: "image", title: "图片", align:"center", width: 100
                , formatter: function(value) {
                    return "<img height='" + size + "' src='" + value + "' />";
                }
            },
            {field: "comboEffect", title: "组合效果", align:"center", width: 100
                , formatter: function(value) {
                    return "<div class='ell'>" + value + "</div>";
                }
            },
            {field: "type", title: "图案类型", align: "center", width: 100
                , formatter: function(value, rowData, rowIndex) {
                    ajaxGet("/potency_fruit_pattern_type/selectById", {
                        id: value
                    }, function(resp) {
                        let data = resp.data;

                        $("#type_" + rowIndex).html(data.name);
                    }, error);

                    return "<div id='type_" + rowIndex + "'></div>";
                }
            }
        ]]
    });

});